C++ pointers are fundamental elements in the language that allow you to work with memory addresses and manipulate data directly in memory. Understanding pointers is crucial for many advanced C++ programming tasks, such as dynamic memory allocation, data structures, and low-level memory operations.
type* pointerName;
Where:
type
is the data type the pointer points to.pointerName
is the name of the pointer variable.Example:
int* intPointer;
double* doublePointer;
You can initialize a pointer to a null value using the nullptr
keyword.
int* nullPointer = nullptr;
To initialize a pointer with the address of an existing variable, you use the address-of operator &
.
int x = 42;
int* pointerToX = &x;
Dereferencing a pointer means accessing the value it points to. You use the dereference operator *
for this purpose.
int value = *pointerToX; // value now contains 42
C++ allows you to perform arithmetic operations on pointers.
n
to a pointer advances it by n
elements of its base type.n
from a pointer moves it back by n
elements.int arr[] = {10, 20, 30, 40, 50};
int* ptr = arr; // Points to the first element (arr[0])
int thirdElement = *(ptr + 2); // Access the third element (30)
In C++, pointers and arrays are closely related. An array name can be used as a pointer to its first element.
int numbers[] = {1, 2, 3, 4, 5};
int* ptr = numbers; // ptr points to numbers[0]
C++ allows you to allocate memory dynamically using new
and deallocate it using delete
.
int* dynamicInt = new int; // Allocate memory for an integer
*dynamicInt = 123; // Assign a value
delete dynamicInt; // Deallocate memory
Pointers can also point to functions, which is useful for implementing callbacks and dynamic function selection.
int add(int a, int b) {
return a + b;
}
int (*funcPointer)(int, int) = add;
int result = funcPointer(2, 3); // result contains 5